<?php
namespace app\Job;

use think\queue\Job;
use think\Db;
use think\facade\Log;
use think\facade\Cache;
use app\services\PHPExcelService;

use app\admin\model\Channelkey;

class SendSelfdomJob{

    public function fire(Job $job, $data){

        trace('执行批次：'.$data['id']);

        print("<info>执行批次:".$data['id']."</info>\n");

        try {

            //目标查询
            $selfdom_info = Db::table('bus_selfdom_info')->where('id',$data['id'])->where('status',0)->find();

            if($selfdom_info)
            {
                $strtime = strtotime($selfdom_info['send_time']) - time();

                //查询时间
                if($strtime > 0)
                {
                    $job->release($strtime);
                    print("<info>延迟:".$strtime."</info>\n");
                    return;
                }
                Db::table('bus_selfdom_info')->where('id',$data['id'])->update(['status'=>1]);

                // 发送短信
                try {

                    $postUrl = '';

                    if($selfdom_info['sms_type'] == 1){
                        $postUrl = 'http://119.91.61.33:8080/SMS/upload/gx/pt';
                    }elseif($selfdom_info['sms_type'] == 2){
                        $postUrl = 'http://119.91.61.33:8080/SMS/upload/gx/sp';
                    }

                    //发送参数
                    $post_data = array(
                        'file' => new \CURLFile(realpath($selfdom_info['save_name']))
                        ,'cid' => $selfdom_info['channel_id']
                    );

                    //发送
                    $curl = curl_init();

                    curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);


                    trace($postUrl . PHP_EOL . "上传数据" . json_encode($post_data,JSON_UNESCAPED_UNICODE),"send");

                    curl_setopt($curl, CURLOPT_URL, $postUrl);

                    curl_setopt($curl, CURLOPT_POST, 1 );

                    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);

                    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

                    curl_setopt($curl, CURLOPT_USERAGENT,"TEST");

                    $result = curl_exec($curl);

                    trace("上传结果" . $result,"send");

                    $error = curl_error($curl);

                    $result = json_decode($result,true);

                    //关闭URL请求
                    curl_close($curl);

                    if($result['code'] == 200){
                        Db::table('bus_selfdom_info')->where('id',$selfdom_info['id'])->update(['batch_num' => $result['order_id']]);
                    }else{
                        trace("上传错误" . $error,"send");
                    }

                } catch (\Exception $e) {
                    trace("第".$key."行发送错误:".$e->getMessage());
                    
                }

            }else{
                trace('没有查到状态为未发送的');
            }
        } catch (\Exception $e) {
            trace($e->getMessage());
        }


        //如果任务执行成功后 记得删除任务，不然这个任务会重复执行，直到达到最大重试次数后失败后，执行failed方法
        $job->delete();

        // 也可以重新发布这个任务
        // $job->release($delay); //$delay为延迟时间
        print("<info>完成</info>\n");
    }

    public function failed($data){

    // ...任务达到最大重试次数后，失败了
    }
}